package svm.coelce.parser.factory;

import java.io.StringWriter;
import java.util.Map;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class QueryFactory {
	public static String gerarConsulta( String consulta, Map<String, Object> parametros ){
		if( consulta != null ){
			VelocityEngine	velocityEngine	= new VelocityEngine();

			VelocityContext	velocityContext	= new VelocityContext();
			StringWriter	stringWriter	= new StringWriter();
			Template		template		= null;

			velocityEngine.setProperty( "resource.loader", "class" );
			velocityEngine.setProperty( "class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
			
			velocityEngine.init();

			if( consulta.equals( "consultaContratosGestor" ) ){
				template = velocityEngine.getTemplate( "velocity/consultas/consultaContratosGestor.vm" );

				velocityContext.put( "unidadeDeControle", parametros.get( "unidadeDeControle" ) );
				velocityContext.put( "digitoVerificador", parametros.get( "digitoVerificador" ) );

				template.merge( velocityContext, stringWriter );
			}

			return stringWriter.toString();
		}

		return null;
	}
}
